var endPlus			= 1;
$(function() {
	//scrollpane parts	
    var scrollPane 		= $( "#slideholder" ),
        scrollContent 	= $( "#reel" );
    var reelPoints 		=[0, -850, -1700, -3200, -4050];
    var sliderPoints 	= [0, 85, 155, 240, 325];
    var pageStatus 		= 0;   
    var prodNum 		= $(".prod").length;
    var step			= Math.floor(100 / (prodNum + endPlus));
	var maxPages 		= Math.floor(prodNum / 4);
	if (prodNum < 5) {
		$('#nav').css("display", "none");
	}
	//RESET SOME CSS
    $("#reel").css({opacity:1}).stop();

    $(function() {
        

        //build slider
        var scrollbar = $( "#bar" ).slider({
            slide: function( event, ui ) {
                if ( scrollContent.width() > scrollPane.width() ) {
                    scrollContent.css( "margin-left", Math.round(
                        ui.value / 100 * ( scrollPane.width() - scrollContent.width() )
                    ) + "px" );
                } else {
                    scrollContent.css( "margin-left", 0 );
                }
            }
        });

        //append icon to handle
        var handleHelper = scrollbar.find( ".ui-slider-handle" )
        .mousedown(function() {
            scrollbar.width( handleHelper.width() );
        })
        .mouseup(function() {
            scrollbar.width( handleHelper.width() );
        })
        .append( "<span class='ui-icon ui-icon-grip-dotted-vertical'></span>" )
        .wrap( "<div class='ui-handle-helper-parent'></div>" ).parent();
        
        //change overflow to hidden now that slider handles the scrolling
        scrollPane.css( "overflow", "hidden" );

        //size scrollbar and handle proportionally to scroll distance
        function sizeScrollbar() {
             var prodNum = $(".prod").length;

            //MAKE REEL WIDTH MATCH HOW MANY PRODUCTS
            var reelWidth = (prodNum+1) * 160 ;
            $("#reel").css("width", reelWidth+'px');

            var remainder = scrollContent.width() - scrollPane.width();
            var proportion = remainder / scrollContent.width();
            var handleSize = scrollPane.width() - ( proportion * scrollPane.width() );
            scrollbar.find( ".ui-slider-handle" ).css({
                width: handleSize,
                "margin-left": -handleSize / 2
            });
            handleHelper.width( "" ).width( scrollbar.width() - handleSize );
        }

        //reset slider value based on scroll content position
        function resetValue() {
            var remainder = scrollPane.width() - scrollContent.width();
            var leftVal = scrollContent.css( "margin-left" ) === "auto" ? 0 :
                parseInt( scrollContent.css( "margin-left" ) );
            var percentage = Math.round( leftVal / remainder * 100 );
            scrollbar.slider( "value", percentage );
        }

        //if the slider is 100% and window gets larger, reveal content
        function reflowContent() {
                var showing = scrollContent.width() + parseInt( scrollContent.css( "margin-left" ), 10 );
                var gap = scrollPane.width() - showing;
                if ( gap > 0 ) {
                    scrollContent.css( "margin-left", parseInt( scrollContent.css( "margin-left" ), 10 ) + gap );
                }
        }

        //change handle position on window resize
        $( window ).resize(function() {
            resetValue();
            sizeScrollbar();
            reflowContent();
        });
               
        // narrow change slide
        if ( scrollContent.width() > scrollPane.width() ) {
        	
        	// Next
        	$("#navNext").click(function(){
                var next 	= scrollbar.slider( "value") + step;
                	next 	= Math.round(next / step) * step;
                	next	= (next > 100) ? 100 : next;
                	
            	var margin 	= Math.round(next / 100 * ( scrollPane.width() - scrollContent.width() ));
                scrollContent.css( "margin-left", margin + "px" );
                scrollbar.slider( "value",  next);                 
            });
        	
        	//Previous
        	$("#navPrevious").click(function(){        	
                var previous = scrollbar.slider( "value") - step;      
                	previous = Math.round(previous / step) * step;
                	previous = (previous < 0) ? 0 : previous;
                	
            	var margin = Math.round(previous / 100 * ( scrollPane.width() - scrollContent.width() ));
            	scrollContent.css( "margin-left", margin + "px" );
                scrollbar.slider( "value",  previous);     
            });
        	
        }
                
        //init scrollbar size
        setTimeout( sizeScrollbar, 10 );//safari wants a timeout
    });


});


$(function() {
	var isOpen = false;
	var prodNum = $(".prod").length;
    //MAKE REEL WIDTH MATCH HOW MANY PRODUCTS
    var reelWidth = (prodNum + endPlus) * 160 ;
    $("#reel").css("width", reelWidth+'px');    

	var howManyProducts = $("#thumbContainer .product-thumb").length;
	
	$('.directionNav').hide();
	
	$("#prod-pull .main").hover(function(){
		if (!isOpen && howManyProducts > 4) {
			$('.directionNav').show();
		}
    }, function(){
        $('.directionNav').hide();
    });	
	
	//alert(howManyProducts);	
    $('#seeAll').click(function() {
    	var maxHeightImg 	= Math.max.apply(Math, $('#thumbContainer img').map(function(){ return $(this).height(); }).get());
    	var prodImg 		= $("#thumbContainer img").length;
    	var plusHeight		= maxHeightImg * Math.ceil(prodImg / 7) + 150;
	    if(!isOpen) {
	        isOpen = true;
			$("#prod-pull #nav").fadeOut(150);
            $("#slideContainer").fadeOut(150);
			$("#thumbContainer").css("top", "0px");
            $("#thumbContainer").animate({height:400}).fadeIn(1000);
            $(".main").animate({height:plusHeight}, 1000);
            if(howManyProducts <= 4) {
            	$('.directionNav').show();
            }            
	    }
	    else {
	        isOpen = false;
			$("#thumbContainer").animate({height:0}).css("display", "none").fadeOut(150, function() {		
				$("#slideContainer").fadeIn(1000);
				$("#prod-pull #nav").fadeIn(1000);
			});		
			$(".main").animate({height:207}, 1000);		
			$('.directionNav').hide();
		}
   });
    
	var defautlImage = 0;
	$("#imageSwap").click(function() {
		if(defaultImage = 0) {
			$("#cts").attr("src", "lifestyles.pixelbridge.com/wp-content/themes/lifestyles/images/change_the_score_launch_placeholder.png" );
			$("#cts").attr("height", "725" );
			defaultImage = 1;
		}
		else {
			$("#cts").attr("src", "lifestyles.pixelbridge.com/wp-content/themes/lifestyles/images/change_the_score_placeholder.png" );
			$("#cts").attr("height", "637" );
			defaultImage = 0;
		}
	});
	
	if(howManyProducts <= 4) {
		$('.directionNav').hide();
		$('#seeAll').css("display", "none");
	}
});

$(function() {
	$("#reel .prod").click(function(){
		var slideId = $(this).attr('id');
		var temp	= slideId.split('_');
		$('.blockProd').fadeOut(300);
		$('#prod_' + temp[1]).fadeIn(1500);
	});
	
});